package controllers

import (
	"encoding/json"
	"github.com/gin-gonic/gin"
	"github.com/unknwon/com"
	"new/kpg/app"
	"new/kpg/e"
	"new/models"
	"new/services"
)

//查找病历表信息
func Getmedical_record(c *gin.Context) {
	err, info, total := services.GetAllmedical_record()
	if err != nil {
		app.Error(c, e.ERROR, err, err.Error())
		return
	}
	app.OK(c, map[string]interface{}{"value": info, "total": total}, "查询成功")
}

//删除病历表信息
func Delmedical_record(c *gin.Context) {
	b, _ := c.GetRawData()
	var m map[string]string
	_ = json.Unmarshal(b, &m)
	id := com.StrTo(m["Mno"]).MustInt()
	if id == -1 {
		app.INFO(c, 30001, "参数错误")
		return
	}
	err := services.Delmedical_record(id)
	if err != nil {
		app.Error(c, e.ERROR, err, err.Error())
		return
	}
	app.OK(c, map[string]interface{}{}, "删除成功")
}

//更改病历表信息
func Updatemedical_record(c *gin.Context) {
	b, _ := c.GetRawData() //获取原始字节
	var m map[string]string
	_ = json.Unmarshal(b, &m) //将结构类型转换为json字符串
	if m["Mno"] == "" {
		app.INFO(c, 30000, "参数非法")
		return
	}

	Mno := m["Mno"]
	Sno := m["Sno"]
	Mresult := m["Mresult"]
	Mmethod := m["Mmethod"]
	Mdate := m["Mdate"]

	if Mno == "" {
		app.INFO(c, 30000, "参数非法")
		return
	}
	err := services.Updatemedical_record(models.Medical_record{Mno, Sno, Mresult, Mmethod, Mdate})
	if err != nil {
		app.Error(c, e.ERROR, err, err.Error())
		return
	}
	app.OK(c, map[string]interface{}{}, "更新成功")
}

//增加病历表信息
func Addmedical_record(c *gin.Context) {
	b, _ := c.GetRawData() //获取原始字节
	var m map[string]string
	_ = json.Unmarshal(b, &m) //将结构类型转换为json字符串

	Mno := m["Mno"]
	Sno := m["Sno"]
	Mresult := m["Mresult"]
	Mmethod := m["Mmethod"]
	Mdate := m["Mdate"]

	if Mno == "" {
		app.INFO(c, 30000, "参数非法")
		return
	}
	err := services.Addmedical_record(models.Medical_record{Mno, Sno, Mresult, Mmethod, Mdate})
	if err != nil {
		app.Error(c, e.ERROR, err, err.Error())
		return
	}
	app.OK(c, map[string]interface{}{}, "添加成功")
}
